//
//  ChatingTableViewCell.m
//  QuanZiChat
//
//  Created by lanou3g on 15/11/25.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "ChatingTableViewCell.h"
#import "MessageFrame.h"
#import "MessageInfo.h"

@interface ChatingTableViewCell()
@property(nonatomic,strong)UILabel *lable;
@property(nonatomic,strong)UIImageView *image;
@property(nonatomic,strong)UIButton *button;

@end

@implementation ChatingTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //显示时间的lable
        UILabel *lable = [[UILabel alloc] init];
        [self.contentView addSubview:lable];
        self.lable = lable;
        //设置字体大小
        lable.font = [UIFont systemFontOfSize:12];
        //文字居中
        lable.textAlignment = NSTextAlignmentCenter;
        
        //显示头像的imageview
        UIImageView *image = [[UIImageView alloc] init];
        [self.contentView addSubview:image];
        self.image = image;
        
        
        //显示正文的按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:button];
        self.button = button;
    }
    //清除cell的背景色
    self.backgroundColor = [UIColor clearColor];
    
    return self;
}



-(void)setMessFrame:(MessageFrame *)messFrame{
    
    _messFrame = messFrame;
    //获取数据模型
    MessageInfo *messageinfo = self.messFrame.messageinfo;
    
    //改变日期显示模式和frame
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *formater = [[NSDateFormatter alloc]init];
    formater.dateFormat = @"yyyy-MM-dd HH:mm";
    self.lable.text = [formater stringFromDate:nowDate];
    self.lable.frame = self.messFrame.timeFrame;
    //设置时间lable是否显示
    self.lable.hidden = messageinfo.hideTime;
    
    
    //设置头像的
    //判断选用谁的头像
    NSString * icon = messageinfo.type == MessageMe ? @"我的.jpg" : @"你的.jpeg";
    self.image.image = [UIImage imageNamed:icon];
    self.image.frame = messFrame.iconFrame;
    
    
    //设置正文的内容
    [self.button setTitle:messageinfo.text forState:UIControlStateNormal];
    self.button.frame = messFrame.textFrame;
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button.titleLabel.numberOfLines = 0;
    //设置titlelable的内边距
    self.button.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 7);
    
    
    //设置正文的背景图
    NSString *imgNor = [[NSString alloc] init];
    NSString *imgHighlighted = [[NSString alloc] init];
    if(messageinfo.type == MessageMe){
        //自己发送的消息
        imgNor = @"balloon_right_blue@2x";
        imgHighlighted = @"balloon_right_blue@2x";
        //设置消息的正文颜色为"白色"
        [self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        //对方发的消息
        imgNor = @"balloon_left_white@2x";
        imgHighlighted = @"balloon_left_white@2x";
        //设置消息的正文颜色为"黑色"
        [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    //加载图片
    UIImage *imageNor = [UIImage imageNamed:imgNor];
    UIImage *imageHighlinghted = [UIImage imageNamed:imgHighlighted];
    //用平铺的方法拉伸图片
    imageNor = [imageNor stretchableImageWithLeftCapWidth: imageNor.size.width/2 topCapHeight:imageNor.size.height/2];
    imageHighlinghted = [imageHighlinghted stretchableImageWithLeftCapWidth:imageHighlinghted.size.width/2 topCapHeight:imageHighlinghted.size.height/2];
    //设置背景图
    [self.button setBackgroundImage:imageNor forState:UIControlStateNormal];
    [self.button setBackgroundImage:imageHighlinghted forState:UIControlStateHighlighted];
}



- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    
}


@end
